package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.dto.NursingProjectPageQueryDto;
import com.zzyl.entity.nursing.NursingProject;
import com.zzyl.entity.nursing.NursingProjectPlan;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author robin
 * @date 24.10.7 17:12
 */
@Service
@RequiredArgsConstructor
public class NursingProjectServiceImpl implements NursingProjectService {
	private final NursingProjectMapper nursingProjectMapper;
	
	@Override
	public PageBean<NursingProjectVo> pageNursingProject(Integer pageNum, Integer pageSize, String name, Integer status) {
		//1-参数校验：pageSize、pageNum
		pageNum = Math.max(pageNum, 1);
		pageSize = Math.max(pageSize, 5);
		//2-分页查询：PageHelper
		PageHelper.startPage(pageNum, pageSize);
		//3-返回结果：PageResponse-->Page<NursingProjectVo>
		Page<NursingProjectVo> page = nursingProjectMapper.selectPage(name, status);
		return PageBean.of(page);
	}
	
	@Override
	public void saveNursingProject(NursingProjectDto nursingProjectDto) {
		//1.插入
		NursingProject nursingProject = new NursingProject();
		BeanUtils.copyProperties(nursingProjectDto, nursingProject);
		nursingProjectMapper.insertOne(nursingProject);
	}
	
	@Override
	public NursingProjectVo queryNursingProjectDetail(Long id) {
		NursingProject nursingProject = nursingProjectMapper.selectOneById(id);
		NursingProjectVo nursingProjectVo = new NursingProjectVo();
		BeanUtils.copyProperties(nursingProject, nursingProjectVo);
		return nursingProjectVo;
	}
	
	@Override
	public void updateNursingProject(NursingProjectDto nursingProjectDto) {
		Long id = nursingProjectDto.getId();
		nursingProjectMapper.updateById(BeanUtil.copyProperties(nursingProjectDto, NursingProject.class));
	}
	
	@Override
	public void enableOrDisableNursingProject(Long id, Integer status) {
		NursingProject nursingProject = new NursingProject();
		nursingProject.setId(id);
		nursingProject.setStatus(status);
		nursingProjectMapper.updateById(nursingProject);
	}
	
	@Override
	public void removeNursingProject(Long id) {
		nursingProjectMapper.deleteById(id);
	}
	
	@Override
	public List<NursingProjectVo> queryAll() {
		return nursingProjectMapper.selectVoList();
	}
	/*
	 * NursingProject
	 * 分页查询
	 * */
//	@Override
//	public PageBean<NursingProject> papegetlist(NursingProjectPageQueryDto nppqd) {
//		PageHelper.startPage(nppqd.getPageNum(), nppqd.getPageSize());
//		Page<NursingProject> list = nursingProjectMapper.papegetlist(nppqd);
//		return PageBean.of(list);
//	}

	
	/*
	 * NursingProject
	 * 分页查询
	 * */
	@Override
	public PageBean<NursingProjectVo> papegetlist(Integer pageNum, Integer pageSize, String name, Integer status) {
		// 开始分页查询
		PageHelper.startPage(pageNum, pageSize);

		// 查询护理项目列表
		Page<NursingProject> page = nursingProjectMapper.selctNursingProjectList(NursingProject.builder().name(name)
				.status(status)
				.build());

		// 获取查询结果列表
		List<NursingProject> nursingProjectList = page.getResult();

		// 使用 CollUtil.isEmpty 工具类判断列表是否为空
		if (CollUtil.isEmpty(nursingProjectList)) {
			// 如果列表为空，返回空的 PageBean
			return PageBean.empty();
		}

		// 查询护理项目的统计信息
		List<Map<String, Object>> mapList = nursingProjectMapper.selctMapList();

		// 将统计信息转换为 Map，键为 projectId，值为 count
		Map<Long, Integer> collect = mapList.stream()
				.collect(Collectors.toMap(
						m -> Long.parseLong(m.get("projectId").toString()),
						m -> Integer.parseInt(m.get("count").toString())
				));
		
		
		// 使用 BeanUtil.copyToList 将 nursingProjectList 转换为 NursingProjectVo 列表
		List<NursingProjectVo> nursingProjectVosList = BeanUtil.copyToList(nursingProjectList, NursingProjectVo.class);

		// 遍历 NursingProjectVo 列表，设置每个项目的 count 属性
		for (NursingProjectVo nursingProjectVo : nursingProjectVosList) {
			nursingProjectVo.setCount(collect.getOrDefault(nursingProjectVo.getId(),0));
		}

		// 返回分页结果
		return PageBean.of(page, nursingProjectVosList);
	}


	@Override
	public void insertgetNursingProject(NursingProjectVo nursingProjectVo) {
		nursingProjectMapper.insertgetNursingProject(nursingProjectVo);
	}

	@Override
	public NursingProjectVo getidlist(Long id) {
		NursingProjectVo nursingProjectVo = nursingProjectMapper.getidlist(id);
		return nursingProjectVo;
	}

	@Override
	public void updatestate(NursingProjectVo nursingProjectVo) {
		nursingProjectMapper.updatestate(nursingProjectVo);
	}

	@Override
	public void getidandstatus(Long id, Integer status) {
		nursingProjectMapper.getidandstatus(id,status);
	}

	@Override
	public void deleteidstatus(Long id) {
		nursingProjectMapper.deleteidstatus(id);
	}

	@Override
	public List<NursingProject> getidnursingStatus() {
		List<NursingProject> nursingproject = nursingProjectMapper.getidnursingStatus();
		return nursingproject;
	}
}
